package com.cea.papyrus.activitydiagram.tabbedproperties.section;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.CallBehaviorAction;

import com.cea.papyrus.activitydiagram.tabbedproperties.composite.ArgumentComposite;
import com.cea.papyrus.activitydiagram.tabbedproperties.composite.BehaviorParametersComposite;
import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.umlutils.Element;

public class CallBehaviorArgumentSection extends PropertyViewSection {
	
	/**
	 * 
	 */
	private BehaviorParametersComposite paramsComposite;
	
	private ArgumentComposite argumentComposite;

	 @Override
		public void createControls(Composite parent,
	            TabbedPropertySheetPage tabbedPropertySheetPage) {
	        super.createControls(parent, tabbedPropertySheetPage);
	        // set a grid Layout
	        GridLayout layout = new GridLayout();
	        layout.numColumns = 2;
	        layout.makeColumnsEqualWidth = true;
	        layout.marginWidth = 0; // remove additional spaces
	        layout.marginHeight = 0; // remove additional spaces
	        parent.setLayout(layout);
	        
	        GridData data = new GridData();
			data.grabExcessHorizontalSpace = true;
			data.horizontalAlignment = SWT.FILL;
			
			// table for Arguments
	        argumentComposite = new ArgumentComposite();
	        argumentComposite.createContent(parent, tabbedPropertySheetPage.getWidgetFactory());
	        argumentComposite.setLayoutData(data);
	        
			// table for Parameters
	        paramsComposite = new BehaviorParametersComposite();
	        paramsComposite.createContent(parent, tabbedPropertySheetPage.getWidgetFactory());
	        paramsComposite.setLayoutData(data);
	        
	        
	    }
	 
	 public void refresh() {
		 argumentComposite.refresh();
		 paramsComposite.setElement(new Element(((CallBehaviorAction)getElement()).getBehavior()));
	    	paramsComposite.refresh();
	    }
	 
	 public void setInput(IWorkbenchPart part, ISelection selection) {
	        super.setInput(part, selection);
	        Assert.isTrue(selection instanceof IStructuredSelection);
	        Object input = ((IStructuredSelection) selection).getFirstElement();
	        if (input instanceof IUMLElementEditPart) {
	        	org.eclipse.uml2.uml.Element element = ((IUMLElementEditPart) input).getUmlElement();
	        	if(element instanceof org.eclipse.uml2.uml.CallBehaviorAction) {
	        		CallBehaviorAction caction=(org.eclipse.uml2.uml.CallBehaviorAction)element;
	        		Element utilElement = new Element(caction);
	        		argumentComposite.setElement(utilElement);
	        		if(caction.getBehavior()!=null){
	        		paramsComposite.setElement(new Element(caction.getBehavior()));}
	            	
	        	}
	        }
	    }
	    
	    /*
	     * (non-Javadoc)
	     * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	     */
	    /**
	     * 
	     */
	    public void dispose(){
	    	super.dispose();
	    	if(paramsComposite!=null)
	    		paramsComposite.dispose();
	    	if(argumentComposite!=null)
	    		argumentComposite.dispose();
	    }
}
